
#include "stdafx.h"
#include "mimosav2client.h"
#include "mimosav2clientDlg.h"


#define	CLOCK_RATE			16000		// 8000, 16000
#define	CHANNEL_COUNT		1
#define	SAMPLES_PER_FRAME	320			// 160, 320
#define	BITS_PER_SAMPLE		16

static struct {
	char				*name;
	pjmedia_tone_desc	tone;	// freq1, freq2, on_msec, off_mesc
} g_Tones[] = {
	{"ringback",	{ 440, 480, 2000, 4000}},	//TONE_RINGBACK
	{"errsound",	{1000,   0,  500,  500}},	//TONE_ERRSOUND
	{"errconnect",	{1000,   0, 1000,  500}},	//TONE_ERRCONNECT
	{"errdialing",	{1000,   0, 2000,  500}},	//TONE_ERRDIALING
};

BOOL Cmimosav2clientDlg::soundCreate()
{
	m_pjPool = pjsua_pool_create("Cmimosav2clientDlg-app", 1000, 1000);
	if (NULL == m_pjPool)
		return FALSE;

	pj_str_t name;
	pj_status_t status;
	for (int i=0; i<SOUND_MAX; i++)
	{
		m_pjTonesID[i] = PJSUA_INVALID_ID;

		name = pj_str(g_Tones[i].name);
		status = pjmedia_tonegen_create2(m_pjPool, &name, CLOCK_RATE, CHANNEL_COUNT, SAMPLES_PER_FRAME, BITS_PER_SAMPLE, PJMEDIA_TONEGEN_LOOP, &m_pjTonesPort[i]);
		if (PJ_SUCCESS == status)
		{
			pjmedia_tonegen_play(m_pjTonesPort[i], 1, &g_Tones[i].tone, PJMEDIA_TONEGEN_LOOP);
			pj_assert(status == PJ_SUCCESS);

			status = pjsua_conf_add_port(m_pjPool, m_pjTonesPort[i], &m_pjTonesID[i]);
			pj_assert(status == PJ_SUCCESS);
		}
	}

	return TRUE;
}

void Cmimosav2clientDlg::soundUpdate(int nType)
{
	static pjsua_conf_port_id st_PortID = PJSUA_INVALID_ID;

	switch (nType)
	{
	case SOUND_RINGBACK:
	case SOUND_ERRSOUND:
	case SOUND_ERRCONNECT:
	case SOUND_ERRDIALING:
		if (PJSUA_INVALID_ID == m_pjConfSlot)
			return;
		{
			if ((PJSUA_INVALID_ID != st_PortID) && (st_PortID != m_pjTonesID[nType]))
				pjsua_conf_disconnect(st_PortID, m_pjConfSlot);

			pjsua_conf_connect(m_pjTonesID[nType], m_pjConfSlot);
			st_PortID = m_pjTonesID[nType];
		}
		break;

	case SOUND_NONE:
		pjsua_set_null_snd_dev();
		st_PortID = PJSUA_INVALID_ID;
		break;

	case SOUND_USB:
		if (PJSUA_INVALID_ID != m_pjConfSlot)
		{
			if (PJSUA_INVALID_ID != st_PortID)
			{
				pjsua_conf_disconnect(st_PortID, m_pjConfSlot);
				st_PortID = PJSUA_INVALID_ID;
			}
		}
		if (PJSUA_INVALID_ID != m_pjCallID)
		{
			int capture_dev = PJSUA_INVALID_ID, playback_dev = PJSUA_INVALID_ID;

			capture_dev = m_cbSoundIn.GetItemData(m_cbSoundIn.GetCurSel());
			playback_dev = m_cbSoundOut.GetItemData(m_cbSoundOut.GetCurSel());
			pj_status_t status = pjsua_set_snd_dev(capture_dev, playback_dev);
			if (PJ_SUCCESS != status)
				logMsg(LOG_SOUND, "!!! %d = pjsua_set_snd_dev(%d, %d) failed !!!", status, capture_dev, playback_dev);

			pjsua_set_ec(0, 0);	// If AEC is disabled, the value will be zero.
		}
		break;
	}

	logMsg(LOG_SOUND, "soundUpdate(%d)", nType);
}

void Cmimosav2clientDlg::soundDestroy()
{
	for (int i=0; i<SOUND_MAX; i++)
	{
		if (PJSUA_INVALID_ID != m_pjTonesID[i])
			pjsua_conf_remove_port(m_pjTonesID[i]);
		if (m_pjTonesPort[i]) 
			pjmedia_port_destroy(m_pjTonesPort[i]);
	}

	if (m_pjPool)
		pj_pool_release(m_pjPool);
}
